package com.vecspace.data.numerical;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

@JsonSerialize(using = HNumericalJsonSerializer.class)  
@JsonDeserialize(using = HMoneyYuanJsonDeserializer.class) 
public class HMoneyYuan extends HMoney{
	private static final long serialVersionUID = 4021619080698151247L;
	
	
	public HMoneyYuan() {
		super();
		formatDigits = 2;
	}
	/**
	 * 
	 * @param value 金额（元）
	 */
	public HMoneyYuan(BigDecimal value) {
		this.setValue(value);
	}

	@Override
	public String getText() {
		String text = HMoney.formatYuan(this.getValue(), this.getSeparatorDigit(), formatDigits);
		return text;
	}

	@Override
	public void setText(String text) {
		BigDecimal num = this.parseDouble(text);
		this.setValue(num);
	}
	
	@Override
	public Class<HMoneyYuan> returnedClass() {
		return HMoneyYuan.class;
	}

	@Override
	public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
			throws HibernateException, SQLException {
		BigDecimal value = rs.getBigDecimal(names[0]);
		HMoneyYuan obj = new HMoneyYuan(value);
		return obj;
	}

	@Override
	public Object deepCopy(Object value) throws HibernateException {
		if(value == null)return null;
		HMoneyYuan obj1 = (HMoneyYuan)value;
		HMoneyYuan obj2 = new HMoneyYuan(obj1.getValue());
		obj2.setSeparatorDigit(obj1.getSeparatorDigit());
		return obj2;
	}

	public HMoneyWanYuan toWanYuan() {
		return new HMoneyWanYuan(this.getValue());
	}
	/**两个数相加，null相当于0
	 * 
	 * @param num1
	 * @param num2
	 * @return 
	 * @author: wanghua
	 */
	public static HMoneyYuan add(HMoneyYuan num1, HMoneyYuan num2) {
		BigDecimal decimal = HNumerical.add((HNumerical)num1, (HNumerical)num2);
		return new HMoneyYuan(decimal);
	}
	
	/**两个数相减，null相当于0
	 * 
	 * @param num1
	 * @param num2 
	 * @author: wanghua
	 */
	public static HMoneyYuan subtract(HMoneyYuan num1, HMoneyYuan num2) {
		BigDecimal decimal = HNumerical.subtract((HNumerical)num1, (HNumerical)num2);
		return new HMoneyYuan(decimal);
	}
		
	/**两个数相乘，遇到null返回null
	 * 
	 * @param num1
	 * @param num2 
	 * @author: wanghua
	 */
	public static HMoneyYuan multiply(HMoneyYuan num1, HNumber num2) {
		BigDecimal decimal = HNumerical.multiply((HNumerical)num1, (HNumerical)num2);
		if(decimal != null) {
			decimal = decimal.setScale(num1.getValue().scale(), RoundingMode.HALF_UP);
		}
		return new HMoneyYuan(decimal);
	}
	
	/**两个数相乘，遇到null返回null
	 * 
	 * @param num1
	 * @param num2 
	 * @author: wanghua
	 */
	public static HMoneyYuan multiply(HMoneyYuan num1, BigDecimal num2) {
		BigDecimal decimal = HNumerical.multiply((HNumerical)num1, num2);		
		return new HMoneyYuan(decimal);
	}
	
	/**两个数相乘，遇到null返回null
	 * 
	 * @param num1
	 * @param num2 
	 * @author: wanghua
	 */
	public static HMoneyYuan multiply(HMoneyYuan num1, HPercent percent) {
		BigDecimal decimal = HNumerical.multiply((HNumerical)num1, (HNumerical)percent);
		if(decimal != null && num1 != null && num1.getValue() != null) {
			decimal = decimal.setScale(num1.getValue().scale(), RoundingMode.HALF_UP);
		}
		return new HMoneyYuan(decimal);
	}
	
	/**两个数相乘，遇到null返回null
	 * 
	 * @param num1
	 * @param num2 
	 * @author: wanghua
	 */
	public static HMoneyYuan multiply(BigDecimal num1, HPercent percent) {
		if(num1 == null)return null;
		if(percent == null || percent.getValue() == null )return null;
		BigDecimal decimal = num1.multiply(percent.getValue());
		if(decimal != null && num1 != null) {
			decimal = decimal.setScale(num1.scale(), RoundingMode.HALF_UP);
		}
		return new HMoneyYuan(decimal);
	}
	
	/**两个数相乘，遇到null返回null
	 * 
	 * @param num1
	 * @param num2 
	 * @author: wanghua
	 */
	public static HMoneyYuan multiply(BigDecimal num1, HNumber num2) {
		if(num1 == null)return null;
		if(num2 == null || num2.getValue() == null)return null;
		BigDecimal decimal = num1.multiply(num2.getValue());
		if(decimal != null && num1 != null) {
			decimal = decimal.setScale(num1.scale(), RoundingMode.HALF_UP);
		}
		return new HMoneyYuan(decimal);
	}
	/**两个数相除，遇到null返回null
	 * 
	 * @param num1
	 * @param num2 
	 * @author: wanghua
	 */
	public static HMoneyYuan divide(HMoneyYuan num1, HNumber num2) {
		BigDecimal decimal = HNumerical.divide((HNumerical)num1, (HNumerical)num2);
		if(decimal != null && num1 != null && num1.getValue() != null) {
			decimal = decimal.setScale(num1.getValue().scale(), RoundingMode.HALF_UP);
		}
		return new HMoneyYuan(decimal);
	}
}
